package com.example.plcopne.ui

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.arcsoft.face.ActiveFileInfo
import com.arcsoft.face.ErrorInfo
import com.arcsoft.face.FaceEngine
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.plcopne.R
import com.example.plcopne.adapter.TestAdapter
import com.example.plcopne.face.common.Constants
import com.example.plcopne.utils.RxUtils
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_test.*

class TestActivity : AppCompatActivity() {
    lateinit var testAdapter: TestAdapter

    val posiname = arrayOf(
        "柜门开关测试",
        "网络连接测试",
        "身份证识别测试",
        "NFC识别测试",
        "人脸识别测试",
        "红外测试",
        "数据测试",
        "柜机充电测试",
        "LED测试"

//        , "摄像头安装测试"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        testAdapter = TestAdapter()


        rl_gride.layoutManager = GridLayoutManager(this, 3)
        rl_gride.adapter = testAdapter
        testAdapter.setNewInstance(posiname.toMutableList())

        testAdapter.setOnItemClickListener { _, _, position ->
            when (position) {
                0, 5 -> {
                    //柜门开关测试
                    startActivity(Intent(this@TestActivity, PLcActivity::class.java))
                }
                1 -> {
                    //网络连接测试
                    val intent = Intent()
                    intent.action = "android.intent.action.VIEW"
                    val content_url: Uri = Uri.parse("https://www.baidu.com")
                    intent.data = content_url
                    startActivity(intent)
                }
                2 -> {
                    //身份证识别测试
                    startActivity(Intent(this@TestActivity, IDCarReadActivity::class.java))
                }
                3 -> {
                    //NFC
                    startActivity(Intent(this@TestActivity, nfcActivity::class.java))
                }
                4 -> {
                    //人脸识别测试
                    activeEngine(null)
                }
                6 -> {
                    //数据测试
                    startActivity(Intent(this@TestActivity, DataTestActivity::class.java))
                }
                7 -> {
                    //柜机充电
                    var wordIntent = Intent(this@TestActivity, WordActivity::class.java)
                    wordIntent.putExtra("type", 7)
                    wordIntent.putExtra("data", "请将柜门开启后,数据线插入USB孔,连接充电设备,确保每一个柜门的充电正常")
                    startActivity(wordIntent)

                }
                8 -> {
                    //LED
                    var wordIntent = Intent(this@TestActivity, WordActivity::class.java)
                    wordIntent.putExtra("data", "请连接卡乐光电APP测试LED")
                    wordIntent.putExtra("type", 8)
                    startActivity(wordIntent)
                }
                9 -> {
                    //摄像头安装测试

                }
            }


        }


    }

    /**
     * 所需的所有权限信息
     */
    private val NEEDED_PERMISSIONS = arrayOf(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_PHONE_STATE
    )


    var isac = true

    /**
     * 激活引擎
     *
     * @param view
     */
    fun activeEngine(view: View?) {
        /**
         * 所需的所有权限信息
         */
        if (!PermissionUtils.isGranted(*NEEDED_PERMISSIONS)) {
            PermissionUtils.permission(*NEEDED_PERMISSIONS)
                .callback(object : PermissionUtils.FullCallback {
                    override fun onGranted(p0: MutableList<String>?) {
                        activ()
                    }

                    override fun onDenied(p0: MutableList<String>?, p1: MutableList<String>?) {
                    }


                })


        } else {
            activ()
        }
    }


    fun activ() {
        if (!isac) {
            showToast("正在激活请稍后")
            return
        }
        isac = false

        Observable.create<Int> { emitter ->
            val runtimeABI = FaceEngine.getRuntimeABI()
            val start = System.currentTimeMillis()
            val activeCode = FaceEngine.activeOnline(
                this@TestActivity,
                Constants.APP_ID,
                Constants.SDK_KEY
            )
            emitter.onNext(activeCode)
        }
            .compose(RxUtils.schedulers())
            .`as`(RxUtils.bindLifecycle(this@TestActivity))
            .subscribe(object : Observer<Int> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(activeCode: Int) {
                    if (activeCode == ErrorInfo.MOK || activeCode == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
                        showToast(getString(R.string.active_success))
                        isac = true
                        startActivity(Intent(this@TestActivity, RegisterAndRecognizeActivity::class.java))
                    } else {
                        showToast(getString(R.string.active_failed, activeCode))
                    }
                    val activeFileInfo = ActiveFileInfo()
                    val res = FaceEngine.getActiveFileInfo(this@TestActivity, activeFileInfo)
                    if (res == ErrorInfo.MOK) {
                    }
                }

                override fun onError(e: Throwable) {
                    showToast(e.message)
                }

                override fun onComplete() {}
            })
    }

    fun showToast(msg: String?) {
        ToastUtils.showShort(msg)
    }
}